//
//  JShowAddressVC.m
//  jhf
//
//  Created by 计海峰 on 2017/9/4.
//  Copyright © 2017年 计海峰. All rights reserved.
//

#import "JShowAddressVC.h"
#import "JNavigationTo.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <MAMapKit/MAMapView.h>
#import <Masonry/Masonry.h>
#import "JLocationInfo.h"

@interface JShowAddressVC ()

@property (nonatomic, strong) MAMapView *mapView;

@property (nonatomic, strong) UIView *detailView;

@property (nonatomic, strong) UILabel *detailLB;

@property (nonatomic, strong) UIButton *navigationBT;

/// 定位按钮
@property (nonatomic, strong) UIButton *locationBT;


@property (nonatomic, strong) UIImageView *detailImageView;

@property (nonatomic, strong) JLocationModel *poi;

@property (nonatomic, assign) CGFloat mapHeight;

@end

@implementation JShowAddressVC

- (instancetype)initWithAMapPOI:(JLocationModel *)poi
{
    self = [super init];
    if (self) {
        _poi = poi;
    }
    return self;
}

- (void)drawView{
    
    self.navigationItem.title = @"查看位置";
    if (_poi.name) {
        self.navigationItem.title = _poi.name;
    }
    
    //初始化地图
    _mapHeight = kScreen_Height - 64 ;
    if (isProfiledScreen) {
        _mapHeight = _mapHeight - 44;
    }
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, _mapHeight)];
    
    [_mapView setZoomLevel:16];
    
    self.mapView.centerCoordinate = CLLocationCoordinate2DMake(_poi.latitude, _poi.longitude);
    
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(_poi.latitude, _poi.longitude);
    
    [_mapView addAnnotation:pointAnnotation];
    
    
    //把地图添加至view
    [self.view addSubview:_mapView];
    [self.view addSubview:self.locationBT];
    
    //添加详情
    [self.view addSubview:self.detailView];
    
    [self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mapView.mas_bottom).offset(0);
        make.left.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(64);
    }];
    
}

//MARK:- 开始定位
- (void)locationAction {
    [[JLocationInfo shared] startLocationWithResult:^(BOOL result) {
        if (result) {
            JLocationInfo *info = [JLocationInfo showLocationInfo:J_LBS_INFO];
            //添加定位点的大头针
            MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
            annotation.coordinate = CLLocationCoordinate2DMake(info.latitude, info.longitude);
            [self.mapView addAnnotation:annotation];
            annotation.lockedToScreen = YES;
            annotation.lockedScreenPoint = CGPointMake(self.mapView.bounds.size.width / 2, self.mapView.bounds.size.height / 2) ;
            //设置地图
            [self.mapView setZoomLevel:15 animated:YES];
            [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(info.latitude, info.longitude) animated:NO];
            self.mapView.showsUserLocation = false;
        }
    }];
}

- (void)setNaviImgName:(NSString *)naviImgName {
    _naviImgName = naviImgName;
    UIImage *img = [UIImage imageNamed:naviImgName];
    if (img) {
        [self.navigationBT setImage:img forState:UIControlStateNormal];
    }
}


//MARK:- 开始导航
- (void)navigation{
    [[JNavigationTo shared] navigationWithPOI:_poi];
}

//MARK: - lazy
- (UIView *)detailView{
    
    if (!_detailView) {
        _detailView = ({
            
            UIView *view = [[UIView alloc] init];
            view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
            
            self.detailLB.frame = CGRectMake(8, 4, kScreen_Width - (64 + 16), 64 - 8);
            [view addSubview:self.detailLB];
            
            self.navigationBT.frame = CGRectMake(kScreen_Width - (64 + 8), 0, 64, 64);
            [view addSubview:self.navigationBT];
            
            view;
        });
    }
    return _detailView;
    
}

- (UILabel *)detailLB {
    if (!_detailLB) {
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = _poi.address;
        label.font = [UIFont systemFontOfSize:15];
        label.numberOfLines = 0;
        _detailLB = label;
    }
    return _detailLB;
}


- (UIButton *)navigationBT {
    if (!_navigationBT) {
        UIButton *button = [[UIButton alloc] init];
        
        [button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize: 15.0];
        
        [button addTarget:self action:@selector(navigation) forControlEvents:UIControlEventTouchUpInside];
        
        NSBundle *mainBundle = [NSBundle bundleForClass:[JShowAddressVC class]];
        NSBundle *resourcesBundle = [NSBundle bundleWithPath:[mainBundle pathForResource:@"Cooper_JMap" ofType:@"bundle"]];
        UIImage *image = [UIImage imageNamed:@"icon_map_navigation_to" inBundle:resourcesBundle compatibleWithTraitCollection:nil];
        [button setImage:image forState:UIControlStateNormal];
        
        button.imageEdgeInsets = UIEdgeInsetsMake(12, 12, 12, 12);
        [button.imageView setContentMode:UIViewContentModeScaleAspectFit];
        _navigationBT = button;
    }
    return _navigationBT;
}

- (UIButton *)locationBT {
    if (!_locationBT) {
        UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.mapView.bounds) - 40, CGRectGetMaxY(self.mapView.frame) - 50, 32, 32)];
        btn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        btn.backgroundColor = [UIColor whiteColor];
        btn.layer.cornerRadius = 3;
        [btn addTarget:self action:@selector(locationAction) forControlEvents:UIControlEventTouchUpInside];
        
        NSBundle *mainBundle = [NSBundle bundleForClass:[JShowAddressVC class]];
        NSBundle *resourcesBundle = [NSBundle bundleWithPath:[mainBundle pathForResource:@"Cooper_JMap" ofType:@"bundle"]];
        UIImage *image = [UIImage imageNamed:@"icon_map_gps_normal" inBundle:resourcesBundle compatibleWithTraitCollection:nil];
        [btn setImage:image forState:UIControlStateNormal];
        _locationBT = btn;
    }
    return _locationBT;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
